import sqlite3, weapon
from math import ceil
   
class Unit:
   def __init__(self, name, pilot=(2,2,1,0)):
      db = sqlite3.connect('blitz.db')
      c = db.cursor()
      c.execute('select * from units where name="%s"' % name)
      q = c.fetchone()
      c.close()
      db.close()

      self.attack = pilot[0]
      self.defense = pilot[1]
      self.ew = pilot[2]
      self.ld = pilot[3]
      self.name = q[0]
      self.actions = q[1]
      self.size = q[2]
      self.detect = q[3]
      self.sensors = q[4]
      self.autocomm = q[5]
      self.comm = q[6]
      self.hasMovement = q[7]
      movementModes = q[7].split(',')
      movementSpeeds = q[8].split(',')
      self.movement = {}
      for i in movementModes:
         self.movement[i] = (0,
                             int(ceil(float(movementSpeeds[movementModes.index(i)]) / 2.0)),
                             int(movementSpeeds[movementModes.index(i)]))
      self.maneuver = q[9]
      self.fireControl = q[10]
      self.armor = q[11]
      self.maxDamageBoxes = q[12]
      self.damageBoxes = q[12]
      self.hasPerk = q[13]
      perks = q[13].split(',')
      perksValues = q[14].split(',')
      self.perks = {}
      for i in perks:
         self.perks[i] = perksValues[perks.index(i)]
      self.hasAux = q[15]
      aux = q[15].split(',')
      auxValues = q[16].split(',')
      self.aux = {}
      for i in aux:
         self.aux[i] = auxValues[aux.index(i)]
      self.hasWeapon = q[17]
      weapons = q[17].split(',')
      weaponArcs = q[18].split(',')
      self.weapons = {}
      for i in weapons:
         self.weapons[i] = weapon.Weapon(i, weaponArcs[weapons.index(i)])

      return